package com.younion.action.manage;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.younion.business.IConfigService;
import com.younion.common.base.BaseAction;
import com.younion.common.constants.ConfigType;
import com.younion.common.constants.Status;
import com.younion.common.paginator.Paginator;
import com.younion.vo.ConfigVO;

import cpcns.jhsks.util.MapCollectionUtils;

/**
 * @author Joe Chen
*/
public class ConfigAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(ConfigAction.class);

	private IConfigService configService = null;

	private ConfigVO configVO = new ConfigVO();
	
	private List<ConfigType> configTypes = Arrays.asList(ConfigType.values());

	private List<ConfigVO> configList;// 结果集列表
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	public String listConfig() {
		try {
			Map paramMap = null;
			if (configVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(configVO));
			}
			// 获取总记录数
			int total = configService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			configList = configService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	public String addConfig() {
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	public String editConfig() {
		try {
			configVO = configService.getById(configVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	public String saveConfig() {
		try {
			configVO.setName(ConfigType.valueOfName(configVO.getCode()).getTitle());
			configVO.setAddtime(new Date());
			configVO.setStatus(Status.ACTIVE.getName());
			configVO = configService.save(configVO);
			String id = configVO.getId() == null ? "" : configVO.getId().toString();
			String log = "".equals(id) ? "添加客户端信息" : "更新客户端信息";
			Integer type = "".equals(id) ? 3 : 4;
			this.addLog(type, log+id, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "costList");
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	public String deleteConfig() {
		try {
			String pkId = request.getParameter("key");
			configService.remove(Arrays.asList(pkId.split(",")));
			this.addLog(5, "删除客户端信息管理", "删除客户端信息管理" ,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "costList");
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	public String viewConfig() {
		try {
			String pkId = request.getParameter("key"); 
			configVO = configService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public ConfigVO getConfigVO() {
		return configVO;
	}

	public void setConfigVO(ConfigVO configVO) {
		this.configVO = configVO;
	}

	public void setConfigService(IConfigService configService) {
		this.configService = configService;
	}

	public List<ConfigVO> getConfigList() {
		return configList;
	}

	public void setConfigList(List<ConfigVO> configList) {
		this.configList = configList;
	}

	public List<ConfigType> getConfigTypes() {
		return configTypes;
	}

	public void setConfigTypes(List<ConfigType> configTypes) {
		this.configTypes = configTypes;
	}

	public IConfigService getConfigService() {
		return configService;
	}

}